﻿Public Class UCQLNhanVien
    Private NhanVienBUS As NhanVienBUS
    Private NhanVienDTO As NhanVienDTO

    'Dim provider As New DataProvider()
    Dim connect As New connect()
    Private mk As String = ""
    Public Sub datpass(ByVal pass As String)
        Me.mk = pass
    End Sub
    Private Sub UCQLNhanVien_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        LoadNhanVien()
        LoadcmbLNV()

    End Sub
    Private Sub LoadcmbLNV()
        Dim ds As New DataSet()
        ds = connect.LoadLNV("select MALNV,TENLNV from LOAINHANVIEN")
        With cmbLNV
            .DataSource = ds.Tables("LOAINHANVIEN")
            .DisplayMember = "TENLNV"
            .ValueMember = "MALNV"
        End With
    End Sub
    Private Sub LoadNhanVien()
        dgvNhanVien.ClearSelection()
        NhanVienBUS = New NhanVienBUS()
        Dim NhanVienList As New List(Of NhanVienDTO)
        NhanVienList = NhanVienBUS.GetData()
        dgvNhanVien.DataSource = NhanVienList
        dgvNhanVien.ClearSelection()
    End Sub

    Private Sub btnSetPass_Click(sender As System.Object, e As System.EventArgs) Handles btnSetPass.Click
        Dim frm As frmSetPass
        Dim MaNV = txtMaNV.Text
        frm = New frmSetPass()
        frm.gan(MaNV)
        frm.ShowDialog()

    End Sub

    Private Sub dgvNhanVien_SelectionChanged(sender As System.Object, e As System.EventArgs) Handles dgvNhanVien.SelectionChanged
        txtMaNV.Text = dgvNhanVien.CurrentRow.Cells(0).Value.ToString()
        txtTenDangNhap.Text = dgvNhanVien.CurrentRow.Cells(1).Value.ToString()
        cmbLNV.SelectedValue = dgvNhanVien.CurrentRow.Cells(2).Value.ToString()
        cbBiXoa.Checked = Convert.ToBoolean(dgvNhanVien.CurrentRow.Cells(3).Value)
        btnThem.Enabled = False
        btnCapNhat.Enabled = True
        btnXoa.Enabled = True
        btnSetPass.Enabled = True
    End Sub
    Private Function TaoMaNV() As String
        Dim maNV As String
        Dim loai As String = cmbLNV.SelectedValue.ToString().Trim()
        Dim dt As New DataTable
        dt = connect.GetData("select MANV from NHANVIEN where MANV like '" + loai + "%'")
        Dim stt As Integer = dt.Rows.Count() + 1
        Dim str_stt As String = stt.ToString("000")

        maNV = loai + str_stt
        Dim check As Boolean = False
        While (check = False)
            Dim dt2 As DataTable = connect.GetData("select * from NHANVIEN where MANV = '" + maNV + "'")
            If (dt2.Rows.Count() > 0) Then
                stt = stt + 1
                str_stt = stt.ToString("000")
                maNV = loai + str_stt
                Dim dt3 As DataTable = connect.GetData("select * from NHANVIEN where MANV ='" + maNV + "'")
                If (dt3.Rows.Count() > 0) Then
                    check = False
                Else
                    check = True
                End If
            Else
                check = True
            End If
        End While
        Return maNV
    End Function
    Private Sub btnThem_Click(sender As System.Object, e As System.EventArgs) Handles btnThem.Click
        Dim tenDN As String = txtTenDangNhap.Text.Trim()
        Dim sqlkt As String = "select TENDANGNHAP from NHANVIEN where TENDANGNHAP='" + tenDN + "'"
        Dim dt As New DataTable
        dt = connect.GetData(sqlkt)
        If (dt.Rows.Count() > 0) Then
            MessageBox.Show("Tên đăng nhập bị trùng!")
            txtTenDangNhap.Focus()
            Return
        End If
        Dim frm As New frmSetPass()
        If (lbMK.Text = "") Then
            frm.trangthai(True)
            MessageBox.Show("Đặt mật khẩu!")
            frm.gan("Đã ẩn")
            frm.ShowDialog()
        Else
            Dim maNV As String = TaoMaNV()
            Dim maLNV As String = cmbLNV.SelectedValue.ToString().Trim()
            Dim biXoa As String = cbBiXoa.Checked.ToString()
            Dim sql As String = "insert into NHANVIEN(MANV,TENDANGNHAP,MATKHAU,MALNV,BIXOA) values('" + maNV + "','" + tenDN + "','" + lbMK.Text + "','" + maLNV + "','" + biXoa + "')"
            connect.ExecSql(sql)
            MessageBox.Show("Thêm thành công!")
            LoadNhanVien()
        End If
    End Sub
    Private Sub LamMoi()
        LoadNhanVien()
        btnThem.Enabled = True
        btnCapNhat.Enabled = False
        btnXoa.Enabled = False
        btnSetPass.Enabled = False

        txtMaNV.Text = ""
        txtTenDangNhap.Text = ""
        cbBiXoa.Checked = False
        cmbLNV.SelectedValue = "B"
    End Sub
    Private Sub btnReset_Click(sender As System.Object, e As System.EventArgs) Handles btnReset.Click
        LamMoi()
    End Sub

    Private Sub btnCapNhat_Click(sender As System.Object, e As System.EventArgs) Handles btnCapNhat.Click
        Dim maNV As String = txtMaNV.Text
        Dim tenDN As String = txtTenDangNhap.Text
        Dim loai As String = cmbLNV.SelectedValue.ToString().Trim()
        Dim biXoa As String = cbBiXoa.Checked.ToString()
        Dim sql As String = "update NHANVIEN set TENDANGNHAP='" + tenDN + "',MALNV='" + loai + "',BIXOA='" + biXoa + "' where MANV='" + maNV + "'"
        connect.ExecSql(sql)
        MessageBox.Show("Lưu thành công!")
        LamMoi()
    End Sub

    Private Sub btnXoa_Click(sender As System.Object, e As System.EventArgs) Handles btnXoa.Click
        If (txtMaNV.Text <> "") Then
            Dim sql As String = "select * from TINDANG where MANV='" + txtMaNV.Text + "'"
            Dim dt As New DataTable
            dt = connect.GetData(sql)
            If (dt.Rows.Count() > 0) Then
                Dim result = MessageBox.Show("Nhân viên " + txtMaNV.Text + " đã đăng tin cho " + dt.Rows.Count().ToString() + " người nên bạn không thể xoá. Bạn có muốn đánh dấu Xoá không.", "Thông báo", MessageBoxButtons.YesNo)
                If result = DialogResult.No Then
                    'Me.Close()
                ElseIf result = DialogResult.Yes Then
                    sql = "update NHANVIEN set BIXOA='True' where MANV='" + txtMaNV.Text + "'"
                    connect.ExecSql(sql)
                    MessageBox.Show("Đánh dấu xoá thành công!")
                    LoadNhanVien()
                    LamMoi()
                End If
            Else
                Dim sqldelete As String = "delete NHANVIEN where MANV='" + txtMaNV.Text + "'"
                connect.ExecSql(sqldelete)
                MessageBox.Show("Xoá thành công!")
                LamMoi()
            End If
        End If
    End Sub
End Class
